package com.rf.richfitwheel.workflow.controller;

import com.alibaba.fastjson.JSON;
import com.rf.richfitwheel.admin.feignclient.FSysUserService;
import com.rf.richfitwheel.admin.sys.model.User;
import com.rf.richfitwheel.admin.sys.vo.UserVO;
import com.rf.richfitwheel.admin.utils.UserUtil;
import com.rf.richfitwheel.common.utils.R;
import com.rf.richfitwheel.common.utils.StringUtils;
import com.rf.richfitwheel.workflow.model.WfProcessTask;
import com.rf.richfitwheel.workflow.service.ModelService;
import com.rf.richfitwheel.workflow.service.WfProcessService;
import com.rf.richfitwheel.workflow.service.WfProcessTaskService;
import net.sf.json.JSONObject;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.repository.Model;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.batik.transcoder.TranscoderException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 流程目录表
 *
 * @author zhanglu
 * @email zhanglu2@cnpc.com.cn
 * @date 2021-07-20 09:01:22
 */
@RestController
@RequestMapping("/wfprocesstask")
public class WfProcessTaskController {
    protected static final Logger log = LoggerFactory.getLogger(WfProcessTaskController.class);

    @Autowired
    private WfProcessTaskService wfProcessTaskService;
    @Autowired
    private WfProcessService wfProcessService;
    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private FSysUserService fSysUserService;

    @Autowired
    private RepositoryService repositoryService;



    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody String params) throws TranscoderException, IOException {
        UserVO userVO = UserUtil.getUser();
        if (StringUtils.isNotBlank(params)) {
            JSONObject jsonObject = JSONObject.fromObject(params);
            String modelId = jsonObject.getString("modelId");
            List<WfProcessTask> wfProcessTasks = JSON.parseArray(jsonObject.getJSONArray("wfProcessTasks").toString(), WfProcessTask.class);
            //System.out.println(wfProcessTasks);
            wfProcessTaskService.updateWfProcessTasksAndPublish(modelId, wfProcessTasks, userVO);

        }
        return R.ok();
    }

}
